var gridList;

$(document).ready(function () {
    gridList = new dataGrid("gridList");
    init_Data();
});
//初始化数据
function init_Data(){
    gridList.load({"activeFlag":2});
}

function formatOper(e) {
    var row=e.record;
    var name = row.formName || '';
    var formId = row.formId || '';

    var btn = "<a class='layui-btn layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='recycle(\"" + formId + "\",\"" + name + "\")'><i class='layui-icon'>&#xe642;</i>恢复</a>" +
        "<a class='layui-btn layui-btn-danger layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='removeData(\"" + formId + "\",\"" + name + "\")'><i class='layui-icon'>&#xe640;</i>删除</span></a>";
    return btn;
}

//回收站
function recycle(id, title) {
    layer.confirm('确定要恢复' + title + '么？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.post("/wf/form/update", {"formId": id,"activeFlag":1}, function (data) {
            if (data.OK == "OK") {
                layer.msg('操作成功！', {
                    icon: 1,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            } else {
                layer.msg('操作失败！', {
                    icon: 2,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            }
        });
    }, function () {
    });
}

//删除
function removeData(id, title) {
    layer.confirm('确定要删除' + title + '么？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.post("/wf/form/deleteById", {"id": id}, function (data) {
            if (data.OK == "OK") {
                layer.msg('操作成功！', {
                    icon: 1,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            } else {
                layer.msg('操作失败！', {
                    icon: 2,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            }
        });
    }, function () {
    });
}

//批量回收
function batchRecycle() {
    var dataStr = gridList.getCheckData();
    var data = JSON.parse(dataStr);
    console.log(data)

    if(data == null || data.length == 0){
        layer.msg('请选择要恢复的模块！', {
            icon: 0,
            time: 1000, //1秒关闭（如果不配置，默认是3秒）
            shade: 0.3
        });
        return;
    }

    var ids = "";
    $.each(data, function (index, temp) {
        ids += (temp.formId+",")
    });
    ids = ids.substring(0, ids.length-1);

    layer.confirm('确定要恢复选中的模块么？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.post("wfForm/recoverData.do", {"id": ids}, function (data) {
            if (data.OK == "OK") {
                layer.msg('操作成功！', {
                    icon: 1,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            } else {
                layer.msg('操作失败！', {
                    icon: 2,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            }
        });
    }, function () {
    });
}